package top.loveyumin1314.music.serviceimpl;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.GlobalEventExecutor;
import top.loveyumin1314.music.bean.*;
import top.loveyumin1314.music.cmd.CreateResponse;
import top.loveyumin1314.music.code.ActionCode;
import top.loveyumin1314.music.lottery.LotteryService;
import top.loveyumin1314.music.service.ClientCallBack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * author Kerry
 * date 2017/10/10
 * desp null
 */

public class BroadcastService implements ClientCallBack {

    public static final ChannelGroup GROUP = new DefaultChannelGroup("CHANNEL_SESSION_NAME", GlobalEventExecutor.INSTANCE);
    public static final AttributeKey<UserActionBean> SISSION = AttributeKey.valueOf("netty.channel");


    private static BroadcastService INSTANCE;

    public static BroadcastService getInstance() {
        if (INSTANCE == null) {
            synchronized (BroadcastService.class) {
                if (INSTANCE == null)
                    INSTANCE = new BroadcastService();
            }
        }
        return INSTANCE;
    }

    private BroadcastService() {
    }

    public UserListBean<UserActionBean> getOnlineUserList(ActionCode actionCode) {
        return getOnlineUserList(null, actionCode);
    }

    public UserListBean<UserActionBean> getOnlineUserList(UserActionBean userActionBean, ActionCode actionCode) {

        UserListBean<UserActionBean> userListBean = new UserListBean<>();
        List<UserActionBean> sessionBeanList = new ArrayList<>();
        for (Channel channel : GROUP) {
            Attribute<UserActionBean> attr = channel.attr(BroadcastService.SISSION);
            UserActionBean session = attr.get();
            if (session != null) {
                sessionBeanList.add(session);
            }
        }
        userListBean.setUserId(userActionBean.getUserId());
        userListBean.setTime(userActionBean.getTime());

        userListBean.setActionId(actionCode.actionId);
        userListBean.setActionDetail(actionCode.actionDetail);
        userListBean.setItemList(sessionBeanList);
        return userListBean;
    }


    public UserListBean<LotteryBean> getLotteryList(UserActionBean userActionBean, ActionCode actionCode) {
        UserListBean<LotteryBean> userListBean = new UserListBean<>();

        List<LotteryBean> values = new ArrayList<>(LotteryService.LOTTERY_USERS.values());

        userListBean.setUserId(userActionBean.getUserId());
        userListBean.setTime(userActionBean.getTime());

        userListBean.setActionId(actionCode.actionId);
        userListBean.setActionDetail(actionCode.actionDetail);
        userListBean.setItemList(values);
        return userListBean;
    }


    @Override
    public void broadcast(CreateResponse response) throws IOException {
        GROUP.writeAndFlush(response.buildWebSocketFrame());
    }

    @Override
    public void broadcast(Object response) throws IOException {
        TextWebSocketFrame textWebSocketFrame = CreateResponse.create().buildWebSocketFrame(response);
        GROUP.writeAndFlush(textWebSocketFrame);
    }
}
